<template>
  <vxe-modal
    ref="commonSelectModal"
    v-model="visible"
    :resize="true"
    width="1024"
    height="600"
    title="添加管理员"
    :show-zoom="true"
    :destroy-on-close="false"
    :showFooter="true"
    @close="cancel"
    :mask="false"
  >
    <div class="common-select-container">
      <div class="searchBox">
        <el-form :inline="true">
          <el-form-item label="姓名：" prop="userName">
            <el-input v-model="search.userName" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="所属部门：" prop="departmentId">
            <el-cascader
              v-model="search.departmentId"
              clearable
              placeholder="请选择"
              :options="deptTree"
              :show-all-levels="false"
              :props="{value:'id',label:'text',checkStrictly:true}"
            ></el-cascader>
          </el-form-item>
          <el-form-item style="margin-top: -2px">
            <el-button type="primary" @click="tableSearch">查询</el-button>
            <el-button type="info" @click="search = {}, tableSearch()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <qm-table
        v-if="visible"
        ref="qmTable"
        :urls="{ query: '/admin/sysUser/query' }"
        :showMenu="false"
        :showToolbar="false"
        :showCheckbox="multiple"
        :showRadio="!multiple"
        :options="calOptions"
        :pagerOptions="{ pageSize: 5 }"
        :beforeQuery="beforeQuery"
      >
        <qm-table-column
          v-for="item in columns"
          :key="item.field"
          :field="item.field"
        >{{ item.name }}</qm-table-column>
      </qm-table>
    </div>

    <template v-slot:footer>
      <el-row>
        <el-col :span="24" style="text-align: right;">
          <el-button type="primary" @click="returnResult">确定</el-button>
          <el-button type="info" @click="cancel">关闭</el-button>
        </el-col>
      </el-row>
    </template>
  </vxe-modal>
</template>

<script>
import request from "@/utils/request";
import _ from "lodash";
export default {
  name: "select-table",
  props: {
    url: String,
    multiple: Boolean,
    value: Boolean,
    onBeforeQuery: Function,
  },
  data() {
    return {
      deptTree: [],
      columns: [
        { name: "登录账号", field: "loginId" },
        { name: "姓名", field: "userName" },
        { name: "用户类型", field: "userType" },
        { name: "所属部门", field: "departmentName" },
      ],
      search: {},
      visible: false,
    };
  },
  computed: {
    calOptions() {
      if (this.multiple) {
        return { checkboxConfig: { highlight: true, trigger: "row" } };
      }
      return {
        checkboxConfig: { highlight: false },
        radioConfig: { highlight: true, trigger: "row" },
      };
    },
  },
  watch: {
    value(visible) {
      this[visible ? "show" : "hide"]();
    },
  },
  methods: {
    tableSearch() {
      let params = {
        start: 0,
        query: {},
      };
      let search = _.clone(this.search);
      if (_.isArray(search.departmentId) && search.departmentId.length > 0) {
        search.departmentId =
          search.departmentId[search.departmentId.length - 1];
      }
      params.query = _.extend({}, search);
      this.$refs["qmTable"].fetchListData(params);
    },
    beforeQuery(params) {
      if (this.onBeforeQuery) {
        this.onBeforeQuery(params);
      }
    },
    show() {
      this.visible = true;
    },
    hide() {
      this.visible = false;
    },
    cancel() {
      this.$emit("cancel");
    },
    returnResult() {
      let rows = this.$refs["qmTable"].getSelectedRows();
      this.$emit("success", rows);
    },
    getDeptTreeData() {
      request({
        url: "/admin/sysDepartment/selectDep",
        method: "get",
        params: {},
      }).then((r) => {
        this.deptTree = r || [];
      });
    },
  },
  created() {
    this.getDeptTreeData();
  },
};
</script>

<style>
</style>
